<?php
namespace app\house\model;
use app\BaseModel;
use think\facade\Db;



class IndexHouse extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_house';
    /**
     * @var array
     * 查询器
     */
    protected $append = [
        'area_text',
        'img_text',
        'index_text',
        'video_vid'
    ];
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 图片
     */
    public function getImgTextAttr($value,$data){
        $data = transImages($data,['imgs'],';');
        return $data['imgs'];
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgInfoAttr($value){
        $img = transImages([$value],['img_info'],';');
        return is_array($img[0])?$img[0]:[];
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 视频封面图
     */
    public function getVideoCoverAttr($value){
        $info =  transImagesOne([$value],[$value]);
        return !empty($info)?$info[0]:'';

    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 视频
     */
    public function getVideoAttr($value){
        $info =  transImagesOne([$value],[$value]);
        return !empty($info)?$info[0]:'';
    }


    /**
     * User: chenniang
     * Date: 2019-11-15 13:34
     * @return void
     * descption:腾讯视频
     */
    public function getVideoVidAttr($value,$data){
        return lbGetTencentVideo($data['video']);
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * vr封面图
     */
    public function getVrCoverAttr($value){
        $info =  transImagesOne([$value],[$value]);
        return !empty($info)?$info[0]:'';
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getImgsAttr($value){
        $img = transImages([$value],['imgs'],';');
        return is_array($img[0])?$img[0]:[];
    }

    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getDesignImgAttr($value){

        $img = transImages([$value],['design_img'],',');

        return is_array($img[0])?$img[0]:[];
    }
    /**
     * @param $value
     * @return array|mixed
     * 转换图片
     */
    public function getRealImgAttr($value){

        $img = transImages([$value],['real_img'],',');

        return is_array($img[0])?$img[0]:[];
    }
    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getBuildAttr($value){

        $build =  json_decode($value,true);

        return $build;
    }

    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getNewHxAttr($value){

        $build =  json_decode($value,true);

        return $build;
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 地区
     */
    public function getAreaTextAttr($value,$data){
        $name = Db::name('longbing_card_house_area')->where(['id' => $data['area_id'], 'uniacid' => $data['uniacid']])->value('title');
        return !empty($name)?$name:'';
    }
    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getAttrsAttr($value){
        $attr =  json_decode($value,true);
        if(!empty($attr['户型'])){
            $attr['户型'] = Db::name('longbing_card_house_huxing')->where(['id' => $attr['户型']])->value('title');
        }
        if(isset($attr['物梯户比例业费'])){
            unset($attr['物梯户比例业费']);
        }
        return $attr;
    }

    /**
     * @param $value
     * @return mixed
     * 转换标签
     */
    public function getLabelsAttr($value){
        $l= explode(',',$value);
        return !empty($l[0])?$l:[];
    }
    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getCommunityAttrsAttr($value){
        $attr =  json_decode($value,true);
        if(isset($attr['物梯户比例业费'])){
            unset($attr['物梯户比例业费']);
        }
        return $attr;
    }

    /**
     * @param $value
     * @return mixed
     * 转换json
     */
    public function getMonomerPathAttr($value){

        $build =  json_decode($value,true);

        return $build;
    }
    /**
     * @param $value
     * @return mixed
     * 转换double
     */
    public function getLatitudeAttr($value){
        return floatval($value);
    }

    /**
     * @param $value
     * @return mixed
     * 转换double
     */
    public function getLongitudeAttr($value){
        return floatval($value);
    }

    /**
     * @param $value
     * @return string
     *  友好时间
     */
    public function getCreateTimeAttr($value){
        return lbGetfDate(time()-$value);
    }



    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 房源列表
     */

    public function houseList($dis,$page=10){
        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * @param $dis
     * 房源详情
     */
    public function houseInfo($dis,$staff_id=''){
        $data = $this->where($dis)->find();

        $house_staff = new AdminHoseStaff();

        if(!empty($data)){

            $one_staff = [];

            if(!empty($staff_id)){
                //查询指定的
                $one_staff[] = $house_staff->getStaffOne($staff_id);
                //查询除开这个员工的
                $house_dis[] = ['a.staff_id','<>',$staff_id];
            }
            $limit = !empty($staff_id)?14:15;
            //该房源的
            $house_dis[] = ['a.house_id','=',$data['id']];
            //推荐员工查询
            $data['house_staff'] = $house_staff->getStaffLimit($house_dis,$limit);
            //合并数组
            $data['house_staff'] = array_merge($one_staff,$data['house_staff']);
        }
        $data = !empty($data)?$data->toArray():$data;
        return $data;

    }


    /**
     * @param $dis
     * 获取id
     */
    public function houseId($dis){
        $data = $this->where($dis)->column('id');
        return $data;

    }


    /**
     * @param $dis
     * @功能说明:浏览记录
     * @author chenniang
     * @DataTime: 2020-03-16 4:31
     */
    public function pvList($dis){

        // 总数
        $data['count'] = Db::name('longbing_card_count')->alias('a')->where($dis)->count();
        //列表
        $data['list']  = Db::name('longbing_card_count')->alias('a')
                        ->join('longbing_card_user b','a.user_id = b.id')
                        ->where($dis)
                        ->group('b.id')
                        ->limit(6)
                        ->column('avatarUrl');
        $data['list']  = array_values($data['list']);
        return $data;
    }



}